<?php
require_once("header.php");

if(!isset($_SESSION[SITE_NAME.'_X_AdMiNCP_XADMINLOGGEDID__XXHST']))
{
	session_unset();
	session_destroy();
    header("location: login.php");
   	exit;
}

require_once("header_layout.php"); //Global Header Layout File.


?>

<div class="module_title">Referrers Stats</div><br />
<?php

if(isset($_SESSION['error']))
{
    echo "<div style=\"font-size:12px; color:#FF0000\">".$_SESSION['error']."</div>";
	unset($_SESSION['error']);
}elseif(isset($_SESSION['msg']))
{
    echo "<div style=\"font-size:12px; color:#0000FF\">".$_SESSION['msg']."</div>";
	unset($_SESSION['msg']);

}
?>

 <table class="listings" cellpadding="0" cellspacing="0">
 <tr style="background:url(images/th_bg.jpg) repeat-x; height:27px">
 <th>REFERRER</th>
 <th>DOWNLOADS</th>
</tr>
<?php
$self = "referrers.php";
$page = 1;
$showPerPage = 20;
if(isset($_GET['page']))
{

    $page = (int) makesafe($_GET['page']);
    $page = abs($page);
}

$offset = ceil($page-1)*$showPerPage;

$sql1 = mysql_query("SELECT COUNT(id) as `count`, referrer  FROM downloads_log WHERE referrer != 'NULL' AND referrer != '' GROUP BY referrer");
if(mysql_num_rows($sql1))
{
   
	$records = mysql_num_rows($sql1);
}


//trough an error message if no record found!
if($records < 1)
{
   
   $msg = "No Referrer Found.";
   echo "<tr style=\"background:#ffffff\"><td colspan=\"2\">$msg</td></tr></table>";
   require_once("footer_layout.php"); //Global Footer Layout File.
   return;
}
?>



<?php
//so how many pages we have?
$pages = ceil($records/$showPerPage);

//check if page is greater then number of pages 
if($page > $pages)
 {
   header("location: $self?page=$pages");
 }


// print the link to access each page

$nav  = '';


$pre=$page-1;
$nex = $page+1;

//making fist last 
if(($page==1) and ($pages == 1))
{
   $first = "";
  $previous = "";
  $last  = "";
  $next  = "";
 
}else
if($page == 1 and $pages > 1)
{
  $first = "";
  $previous = "";
  $last  = " <a href=$self?page=$pages>Last</a> ";
  $next  = "<a href=$self?page=$nex>Next</a> | ";
}else if($page == $pages and $pages > 1)
{
 
 $first = "<a href=$self?page=1>First</a> | ";
  $previous = "<a href=$self?page=$pre>Previous</a> ";
  $last  = "";
  $next  = "";
 
} else 
{
   $first = "<a href=$self?page=1>First</a> | ";
  $previous = "<a href=$self?page=$pre>Previous</a> - ";
  $last  = "<a href=$self?page=$pages>Last</a> ";
  $next  = "<a href=$self?page=$nex>Next</a> | ";
}

$query = mysql_query("SELECT COUNT(id) as `count`, referrer  FROM downloads_log WHERE referrer != 'NULL'  AND referrer != '' GROUP BY referrer ORDER BY count DESC LIMIT $offset, $showPerPage") or die(mysql_error());
$x = 1;
while($row = mysql_fetch_object($query))
{

   	 if($x%2 == 0)
	 {
		$trColor = "f1f0f0";	
  	 }else
	 {
		$trColor = "ffffff";	
 	 }
	 
	 
 		$ref_name = stripslashes($row->referrer);
		$ref_name = str_replace("http://", "", $ref_name);
		if(strpos($ref_name, "/") > 0){
		//$ref_name = substr($ref_name, 0, strpos($ref_name, "/"));
		}
	 
	 ?>
	 <tr style="background:#<?=$trColor?>"><td><?=$ref_name?></td><td><?=$row->count?> Downloads</td></tr>
	 <?php
	 $x++;
	 
}


?>
</table>

<br />

<div class="right"><div class="paginginfo">
<b><?=$records?></b> Referrer(s), <b><?=$page?></b> of <b><?=$pages?></b> Page(s).  <?=$first?><?=$previous?><?=$next?><?=$last?>
</div></div>
</div>



<?php

require_once("footer_layout.php"); //Global Footer Layout File.

?>

